package com.lzy.cloud.search.client.util;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.stereotype.Component;

import java.net.InetAddress;

/**
 * Created by WIN7 on 2018/5/21.
 */
@Component
public class EsClient {
  //  private EsClient client = new EsClient();
    TransportClient client = null;

    public EsClient() {
        try {
            Settings settings = Settings.builder()
                    .put("client.transport.sniff", true)
                    .put("cluster.name", "elasticsearch").build();
            client = new PreBuiltTransportClient(Settings.EMPTY)
                    .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.88.212"), 9300));

        } catch (Exception ex) {
            client.close();
        } finally {

        }
    }

    public TransportClient getConnection() {

        if (client == null) {
            synchronized (EsClient.class) {
                if (client == null) {
                    new EsClient();
                }
            }
        }
        return client;

    }
}
